# OddEvenEncryptionFunctionsWhileEmptyString.py # # Description: Write a program that encrypts and decrypt messages # using a transposition algorithm called "odd&even". # Functions: encrypt() returns # decrypt() returns # Looping until the user stops. # # Anne Lavergne # Date: Feb. 5 2024 # ***Encryption*** def encrypt(aPlainMsg): """encrypts a plain message using the transposition algorithm called "odd&even" and returns its encrypted version.""" # Create two empty strings strOfOddChars = "" strOfEvenChars = "" # Start with position (i.e., index) 0 charIndex = 0 # Consider each character in the user's plain message for aChar in aPlainMsg: # If index of character is even if charIndex % 2 == 0: # Put this character in strOfEvenChars strOfEvenChars += aChar # Otherwise put it in strOfOddChars else: strOfOddChars += aChar # Go to the next position (i.e., index) in userPlainMsg charIndex += 1 # When finish, create the cipher # cipherMsg = strOfOddChars + strOfEvenChars return strOfOddChars + strOfEvenChars # ***Decryption*** def decrypt(cipherMsg): """decrypts a cipher message that was encrypted using the transposition algorithm called "odd&even" and returns its decrypted version.""" # Find the middle of cipherMsg middleOfCipherMsg = len(cipherMsg) // 2 # Store the first half of cipherMsg into a string -> oddChars oddChars = cipherMsg[:middleOfCipherMsg] # Store the last half of cipherMsg into a string -> evenChars evenChars = cipherMsg[middleOfCipherMsg:] # Set backToPlainMsg to an empty string backToPlainMsg = "" # Then merge these two strings to recreate plainMsg # Consider each index from 0 to index at the middle of cipherMsg for index in range(middleOfCipherMsg): # Concatenate the character at this index in evenChars with backToPlainMsg backToPlainMsg = backToPlainMsg + evenChars[index] # Concatenate the character at this index in oddChars with backToPlainMsg backToPlainMsg = backToPlainMsg + oddChars[index] # if one half is longer than the other ... if len(oddChars) < len(evenChars): # concatenate this last character to backToPlainMsg backToPlainMsg = backToPlainMsg + evenChars[-1] return backToPlainMsg # *** Main part of my program *** # Ask the user for an other message (or empty string to stop) instruction = 'Please, enter a message to encrypt/decrypt' howToStop = 'simply press the ENTER key to stop' userPlainMsg = input(f'{instruction} ({howToStop}): ') # While the user wants to encrypt/decrypt a message while userPlainMsg: # Encrypt the user plain message by calling the encrypt function returnedCipherMsg = encrypt(userPlainMsg) # Print the cipherMsg, i.e., the encrypted user message print(f'\n"{userPlainMsg}" becomes "{returnedCipherMsg}" once encrypted!') # Decrypt the cipher message by calling the decrypt function returnedPlainMsg = decrypt(returnedCipherMsg) # Print the returnedPlainMsg, i.e., the decrypted message print(f'''\nThe original plain message you entered was "{userPlainMsg}". Once encrypted, your message looked like this "{returnedCipherMsg}". Once this encrypted message was decrypted, we got "{returnedPlainMsg}"''') if userPlainMsg == returnedPlainMsg: print(f'And it is exactly what you originally entered! Yay!') else: print(f'And it is not quite what you originally entered! Oops!') # Ask the user for an other message (or empty string to stop) userPlainMsg = input(f'\n{instruction} ({howToStop}): ') print("Bye!")